// import passport from 'koa-passport'
// import LocalStrategy from 'passport-local'
// import UserModel from '../../dbs/models/users'
const passport = require('koa-passport')
const LocalStrategy = require('passport-local')
const UserModel = require('../../dbs/models/users')

const mysqlp = require('../../api/utils/mysql-p')


passport.use(new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password'
  },
  async function(email,password,done){
  let where = {
    email
  };

  // let result = await UserModel.findOne(where)
  let result = await mysqlp.query(`select * from user where email='${email}';`)
  // console.log('result', result)
  // console.log('result', result[0])
  if(result[0]!=null){
    if(result[0].password === password){
      return done(null,result[0])
    }else{
      return done(null,false,'密码错误')
    }
  }else{
    return done(null,false,'用户不存在')
  }
}))
passport.serializeUser(function(user,done){
  done(null,user)
})

passport.deserializeUser(function(user,done){
  done(null,user)
})

module.exports = passport
// export default passport
